//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by 周小伟 on 16/8/21.
//  Copyright © 2016年 周小伟. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    @objc private func composeDidClick(){
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        presentViewController(nav, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        //Add subview controller
        self.addChildViewControllers()
        
        //classForCoder print the class's name of the function
        print(tabBar.classForCoder)
        
        mainTabBar.composeBtn.addTarget(self, action: #selector(MainViewController.composeDidClick), forControlEvents: .TouchUpInside)
    }
    
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DescoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        let nav = BaseNavViewController(rootViewController: vc)
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //This function is too complex!
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        addChildViewController(nav)
    }
}
